package com.mpsc.spring.demo.listener;

import com.mpsc.spring.demo.event.SendMsgEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @TODO //定义事件监听器，实现ApplicationListener
 * @Author Lensen
 * @Date 2018/7/22
 * @Description
 */
@Component
public class MsgListener implements ApplicationListener<SendMsgEvent> {

    @Async
    @Override
    public void onApplicationEvent(SendMsgEvent sendMsgEvent) {
        sendMsgEvent.output();
        System.out.println(Thread.currentThread().getName());
        System.out.println(sendMsgEvent.receiver + "received msg : " + sendMsgEvent.content);
    }

    //监听有新事务注入，开始执行,此方法无需实现AppllicationListener接口
//    @TransactionalEventListener(fallbackExecution = true, classes = SendMsgEvent.class)
//    @Async
//    @Order(0)//这里是注意注入的顺序
//    public void eventLister(SendMsgEvent sendMsgEvent) {
//        //编写业务逻辑
//        sendMsgEvent.output();
//        System.out.println(Thread.currentThread().getName());
//        System.out.println(sendMsgEvent.receiver + "received msg : " + sendMsgEvent.content);
//    }
}
